package com.test.qpbgboot.base.config;

import com.test.qpbgboot.base.quartz.SchedulingJob;
import com.test.qpbgboot.base.quartz.RefreshScheduleTableJob;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@Slf4j
public class QuartzConfig {

    @Value("${quartz.cron.scheduling-job}")
    private String schedulingJobCron;
    @Value("${quartz.cron.refresh-schedule-table-job}")
    private String refreshScheduleTableJobCron;

    @Bean
    public JobDetail schedulingJobDetail() {
        log.debug("自动排班-定时任务-创建 schedulingJobDetail");
        return  JobBuilder.newJob(SchedulingJob.class)
                .withIdentity("schedulingJobDetail", "SCHEDULING_JOB")
                .storeDurably()
                .build();
    }

    @Bean
    public Trigger schedulingJobTrigger() {
        return TriggerBuilder.newTrigger()
                .forJob(schedulingJobDetail())
                .withIdentity("schedulingJobTrigger", "SCHEDULING_JOB_TRIGGER")
                .withSchedule(CronScheduleBuilder.cronSchedule(schedulingJobCron))
                .build();
    }

    @Bean
    public JobDetail refreshScheduleTableJobDetail() {
        log.debug("自动刷新排班表-定时任务-创建 refreshScheduleTableJobDetail");
        return  JobBuilder.newJob(RefreshScheduleTableJob.class)
                .withIdentity("refreshScheduleTableJobDetail", "REFRESH_SCHEDULE_TABLE_JOB")
                .storeDurably()
                .build();
    }

    @Bean
    public Trigger refreshScheduleTableJobTrigger() {
        return TriggerBuilder.newTrigger()
                .forJob(refreshScheduleTableJobDetail())
                .withIdentity("refreshScheduleTableJobTrigger", "REFRESH_SCHEDULE_TABLE_JOB_TRIGGER")
                .withSchedule(CronScheduleBuilder.cronSchedule(refreshScheduleTableJobCron))
                .build();
    }
}
